package programer.interviewer.link;

public class Question12 {
    public ListNode partition(ListNode head, int x) {
        if (head == null || head.next == null) {
            return head;
        }

        ListNode curNode = head;
        ListNode minNode = new ListNode(Integer.MIN_VALUE);
        ListNode maxNode = new ListNode(Integer.MIN_VALUE);
        ListNode curMin = minNode;
        ListNode curMax = maxNode;

        while(curNode != null) {
            if (curNode.val < x) {
                curMin.next = curNode;
                curNode = curNode.next;
                curMin = curMin.next;
                curMin.next = null;
            } else {
                curMax.next = curNode;
                curNode = curNode.next;
                curMax = curMax.next;
                curMax.next = null;
            }
        }

        curMin.next = maxNode.next;
        return minNode;
    }

    private void printNode(ListNode listNode) {
        if (listNode == null) {
            System.out.println("printNode null");
        } else {
            System.out.println("printNode listNode:" + listNode + " nodevalue:" + listNode.val);
        }
    }
    public static void main(String[] args) {
        ListNode head = new ListNode(1);
        ListNode node3 = new ListNode(3);
        ListNode node4 = new ListNode(4);
        ListNode node2 = new ListNode(2);
        ListNode node22 = new ListNode(2);
        node22.next = null;
        ListNode node5 = new ListNode(5);
        node5.next = node22;
        node2.next = node5;
        node3.next = node2;
        node4.next = node3;

        head.next = node4;
        new Question12().partition(head, 3);
    }
}
